module MapUtils

  def set_list_map
    @map = get_map("gmap")
    @e_regions = Array.new
    @ecases.each do |e|
       if r = e.ecase_region
           @min_x = (@min_x.nil? || @min_x > r.start_x )? r.start_x : @min_x
           @min_x = (@min_x > r.end_x )? r.end_x : @min_x
           @max_x = (@max_x.nil? || @max_x < r.end_x ) ? r.end_x : @max_x
           @max_x = (@max_x < r.start_x ) ? r.start_x : @max_x
           @min_y = (@min_y.nil? || @min_y > r.start_y )? r.start_y : @min_y
           @min_y = (@min_y > r.end_y )? r.end_y : @min_y
           @max_y = (@max_y.nil? || @max_y < r.end_y ) ? r.end_y : @max_y
           @max_y = (@max_y < r.start_y ) ? r.start_y : @max_y 
           @e_regions <<  ("["+ r.start_x.to_s + ","+ r.end_x.to_s + ","+ r.start_y.to_s + ","+ r.end_y.to_s + "]")
       end
    end
    #if @e_regions.size > 0
     # @map_center_x = (@max_x + @min_x)/ 2.0
      #@map_center_y = (@max_y + @min_y)/ 2.0
      #@map.center = [@map_center_y , @map_center_x ]
    #end 
  end
  
  def set_map region_
    @map = get_map("gmap")
    @region = region_
    if @region
      @map_center_x = (@region.end_x + @region.start_x)/ 2.0
      @map_center_y = (@region.end_y + @region.start_y)/ 2.0
      @map.center = [@map_center_y , @map_center_x ]
    end 
  end

  def set_map_extended
    @map = get_map("gmap")
    if @region
      @regions_collection = Array.new
      if @ecase_region
        events = @ecase.events
        events.each do |e|
          if r = e.region                  
            if  float_is_between(r.start_x,@ecase_region.start_x,@ecase_region.end_x) && \
              float_is_between(r.end_x,@ecase_region.start_x,@ecase_region.end_x) && \
              float_is_between(r.start_y,@ecase_region.start_y,@ecase_region.end_y) && \
              float_is_between(r.end_y,@ecase_region.start_y,@ecase_region.end_y) 
              @regions_collection <<  ("["+ r.start_x.to_s + ","+ r.end_x.to_s + ","+ r.start_y.to_s + ","+ r.end_y.to_s + "]")
            end   
          end
        end
      end
      @map_center_x = (@region.end_x + @region.start_x)/ 2.0
      @map_center_y = (@region.end_y + @region.start_y)/ 2.0
      @map.center = [@map_center_y , @map_center_x ]
    end 
  end

end